package ar.edu.unlp.info.tesis.controladores.lrs;

import java.util.Date;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ar.edu.unlp.info.tesis.controladores.lrs.exceptions.StatementException;
import ar.edu.unlp.info.tesis.database.service.ActorService;
import ar.edu.unlp.info.tesis.domain.Agent;
import ar.edu.unlp.info.tesis.domain.Person;


@Controller
@RequestMapping(value = "/agents")
public class AgentProfileAPIController extends ExperienceAPIController{

	@RequestMapping(method = RequestMethod.GET, headers = "Accept=application/json")
	@ResponseBody
	public ResponseEntity<Person> get(@RequestParam(value="agent", required=true) Agent agent) throws Exception
	{
		ActorService service = new ActorService();

		if(agent.getId() != null && !agent.getId().trim().equals(""))
			service.addEqualRestriction("id", agent.getId(), false);
		
		if(agent.getMbox() != null && !agent.getMbox().trim().equals(""))
			service.addEqualRestriction("mbox", agent.getMbox(), false);
		
		Agent a = (Agent) service.uniqueResult();
		
		Person p;
		
		if(a != null)
			p = new Person(a.getId(), a.getName(), a.getMbox());
		else 
			p = new Person();
		
		return new ResponseEntity<Person>(p, HttpStatus.OK);
	}
	
	@RequestMapping(value="/profile", method = RequestMethod.GET, headers = "Accept=application/json")
	@ResponseBody
	public ResponseEntity<Object> getProfile(@RequestParam(value="agent", required=true) Agent agent, @RequestParam(value="profileId") String profileId, @RequestParam(value="since", required=false) Date since) throws Exception
	{
		if(profileId == null && since == null)
			throw new StatementException("You must specified 'profileId' or 'since' parameter", HttpStatus.BAD_REQUEST);
		
		return new ResponseEntity<Object>(HttpStatus.OK);
	}
	
	@RequestMapping(value="/profile", method = RequestMethod.PUT, headers = "Accept=application/json")
	@ResponseBody
	public ResponseEntity<String> putProfile(@RequestParam(value="agent", required=true) Agent agent, @RequestParam(value="profileId", required=true) String profileId) throws Exception
	{
		return new ResponseEntity<String>(HttpStatus.NO_CONTENT);
	}
	
	@RequestMapping(value="/profile", method = RequestMethod.POST, headers = "Accept=application/json")
	@ResponseBody
	public ResponseEntity<String> postProfile(@RequestParam(value="agent", required=true) Agent agent, @RequestParam(value="profileId", required=true) String profileId) throws Exception
	{
		return new ResponseEntity<String>(HttpStatus.NO_CONTENT);
	}
	
	@RequestMapping(value="/profile", method = RequestMethod.DELETE, headers = "Accept=application/json")
	@ResponseBody
	public ResponseEntity<String> deleteProfile(@RequestParam(value="agent", required=true) Agent agent, @RequestParam(value="profileId", required=true) String profileId) throws Exception
	{
		return new ResponseEntity<String>(HttpStatus.NO_CONTENT);
	}
}
